<?php 
include_once "functions.inc";

/**
 *
 * @staticvar array $postVars
 * @param SearchDAO $dao 
 */
function processPostData($dao) {
	static $postVars = array(
		// DB column, post field, search type
		array('nom', 'liv_nom', SearchFilter::TYPE_LIKE),
		array('auteur', 'liv_auteur', SearchFilter::TYPE_LIKE),
		array('ISBN', 'liv_ISBN', SearchFilter::TYPE_LIKE),
		array('edition', 'liv_ed', SearchFilter::TYPE_LIKE),
		array('collection', 'liv_col', SearchFilter::TYPE_LIKE),
		array('location_ID', 'liv_location', SearchFilter::TYPE_EQUAL),
		array('etat_ID', 'liv_etat', SearchFilter::TYPE_EQUAL),
		array('genre_ID', 'liv_genre', SearchFilter::TYPE_EQUAL),
		array('motclefs', 'liv_motclefs', SearchFilter::TYPE_LIKE),
		array('resume', 'liv_resume', SearchFilter::TYPE_LIKE)
	);
	
	foreach ($postVars as $postVar) {
		if (isset($_POST[$postVar[1]])) {
			if ($postVar[0] == 'ISBN')
				$value = return_isbn($_POST[$postVar[1]]);
			else
				$value =  $_POST[$postVar[1]];
			$dao->addFilter ($postVar[0], $value, $postVar[2]);
		}
		else
			$dao->removeFilter ($postVar[0]);
	}
	if (isset($_POST['liv_prete']) && ($_POST['liv_prete'] != '0')) 
		$dao->setIncludeLoaned(TRUE);
	else
		$dao->setIncludeLoaned(FALSE);
		
	if (isset($_POST['liv_pas_prete']) && ($_POST['liv_pas_prete'] != '0')) 
		$dao->setIncludeNotLoaned(TRUE);
	else
		$dao->setIncludeNotLoaned(FALSE);
	
	if (isset($_POST['sort_col']) && !empty($_POST['sort_col'])) 
		$dao->setSortCol($_POST['sort_col']);
}

/**
 * @param SearchDAO $dao 
 */
function displaySearchParams($dao) {
	$genres    = get_genres();
	$etats     = get_etats();
	$locations = get_locations();
?>

<script type="text/javascript" src="js/bsn.AutoSuggest_2.1.3_min.js" charset="utf-8"></script>
<link rel="stylesheet" href="css/autosuggest_inquisitor_min.css" type="text/css" media="screen" charset="utf-8" />

<br/>
<center>
<form name="search_form" METHOD="POST">
<input type="hidden" name="sort_col" value="">
<table id="tabsearch">
  <TR>
	 <TD colspan="2">
      <?php echo _("Titre"); ?>&nbsp;:
      <INPUT TYPE="text" NAME="liv_nom" id="liv_nom" size="25" <?php if ($nom = $dao->getFilter('nom')) echo 'value="'.$nom.'"'; ?> >
    </TD>
	 <TD colspan="2">
	   <?php echo _("Auteur"); ?>&nbsp;:
	   <INPUT TYPE="text" NAME="liv_auteur" id="liv_auteur" size="15" <?php if ($auteur = $dao->getFilter('auteur')) echo 'value="'.$auteur.'"'; ?> >
    </TD>
  </tr>
  <tr>
    <TD>
	   <?php echo _("ISBN"); ?>&nbsp;:
	   <INPUT TYPE="text" NAME="liv_ISBN" id="liv_ISBN" size="15" <?php if ($isbn = $dao->getFilter('ISBN')) echo 'value="'.$isbn.'"'; ?> >
    </TD>
	 <TD>
	   <?php echo _("Edition"); ?>&nbsp;:
	   <INPUT TYPE="text" NAME="liv_ed" id="liv_ed" size="15" <?php if ($edition = $dao->getFilter('edition')) echo 'value="'.$edition.'"'; ?> >
    </TD>
    <TD>
	   <?php echo _("Collection"); ?>&nbsp;:
	   <INPUT TYPE="text" NAME="liv_col" id="liv_col" size="15" <?php if ($collection = $dao->getFilter('collection')) echo 'value="'.$collection.'"'; ?> >
    </TD>
    <TD>
    <input type="checkbox" name="liv_prete" value="1"
    <?php 
    	if ($dao->getIncludeLoaned()) 
			echo ' checked="yes" />'; 
    	else
			echo ' />';
    	echo " : "._("Pr&ecirc;t&eacute;");	
    ?> 
    </TD>
  </TR>
  <TR>
    <TD>
    <?php echo _("Genre"); ?>&nbsp;:
	<select NAME = "liv_genre">
        <option VALUE=""
        <?php
          if (!$dao->getFilter('genre_ID'))
            echo ' selected="selected"';
	      echo ">"._("[non sp&eacute;cifi&eacute;]");
          echo '</option>';
          if ($genres) {
			foreach ($genres as $id => $value) {
				echo '<option VALUE="'.$id.'"';
				if ($dao->getFilter('genre_ID') == $id)
				    echo ' selected="selected"';
				echo '>'.$value.'</option>';
            }
          }
        ?>
      </select>
    </TD>
    <TD>
	<?php echo _("Etat"); ?>&nbsp;:
	<select NAME = "liv_etat">
        <option VALUE=""
        <?php
        if (!$dao->getFilter('etat_ID'))
          echo ' selected="selected"';
		echo ">"._("[non sp&eacute;cifi&eacute;]");
        echo '</option>';

		if ($etats != false) {
			foreach ($etats as $etat_id => $etat_l ) {
				echo '<option VALUE="'.$etat_id.'"';
				if ($dao->getFilter('etat_ID') == $etat_id)
					echo ' selected="selected"';
				echo '>'.$etat_l.'</option>';
			}
		}
        ?>
      </select>
    </TD>
    <TD>
      <?php echo _("Location"); ?>&nbsp;:
	    <select NAME = "liv_location">
        <option VALUE=""
        <?php
        if (!$dao->getFilter('location_ID'))
          echo ' selected="selected"';
	    echo ">"._("[non sp&eacute;cifi&eacute;]");
        echo '</option>';
        
        if ($locations) {
          foreach ($locations as $id => $value) {
            echo '<option VALUE="'.$id.'"';
            if ($dao->getFilter('location_ID') == $id)
                echo ' selected="selected"';
            echo '>'.$value.'</option>';
          }
        }
        ?>
      </select>
    </TD>
    <TD>
    <input type="checkbox" name="liv_pas_prete" value="1"
    <?php 
    	if ($dao->getIncludeNotLoaned()) 
    		echo ' checked="yes" />'; 
    	else
    		echo ' />';
    	echo " : "._("Non pr&ecirc;t&eacute;");	
    ?> 
    </TD>
  </TR>
  <TR>
	  <TD COLSPAN=2>
	    <?php echo _("Mots Clefs"); ?>&nbsp;:
		<INPUT TYPE="text" NAME="liv_motclefs" size="35" <?php if ($motclefs = $dao->getFilter('motclefs')) echo 'value="'.$motclefs.'"'; ?> >
    </TD>
	  <TD COLSPAN=2>
	    <?php echo _("Resum&eacute;"); ?>&nbsp;:
	    <INPUT TYPE="text" NAME="liv_resume" size="35" <?php if ($resume = $dao->getFilter('resume')) echo 'value="'.$resume.'"'; ?>>
    </TD>
  </TR>
  <TR>
    <td colspan="4" align="center">
		<a href="javascript:submitform('search_form','');">
			<span id="searchbtn" ><?php echo _('Rechercher'); ?></span>
		</a>
	</td>
  </TR>
</table>
</form>
</center>
<HR>

<script type="text/javascript">
	
	window.onload = function() {
		new biblioSuggest('liv_nom', 5000);
		new biblioSuggest('liv_auteur', 5000);
		new biblioSuggest('liv_ISBN', 5000);
		new biblioSuggest('liv_ed', 5000);
		new biblioSuggest('liv_col', 5000);
    };

</script>
<?php
}
?>
